package com.gxa.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {

    @Bean
    public Docket docketDemo1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.payment.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("缴费管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }
    @Bean
    public Docket docketDemo2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.community.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("车行管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }
    @Bean
    public Docket docketDemo3() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.communityManagement.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("资料管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }
    @Bean
    public Docket docketDemo4() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.property.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("物业管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }
    @Bean
    public Docket docketDemo5() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.deviceManager.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("设备管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }

    @Bean
    public Docket docketDemo6() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.sys.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("系统设置")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }

    @Bean
    public Docket docketDemo7() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.datamanagement.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("小区管理")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }

    @Bean
    public Docket docketDemo8() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.gxa.modules.worktable.controller"))
                .paths(PathSelectors.any()) //正则匹配请求路径，并分配至当前分组
                .build()
                .groupName("工作台")   //分组名称
                .securitySchemes(security());
//                .ignoredParameterTypes(Demo.class)

    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            //加了ApiOperation注解的类，才生成接口文档
//            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //包下的类，才生成接口文档
            .apis(RequestHandlerSelectors.basePackage("com.gxa.modules"))
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(security());
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("xx-project")
            .description("xx-project文档")
            .termsOfServiceUrl("https://www.baidu.com")
            .version("3.0.0")
            .build();
    }

    private List<ApiKey> security() {
        List<ApiKey> keys = new ArrayList<>();
        ApiKey apiKey = new ApiKey("token", "token", "header");
        keys.add(apiKey);
        return keys;
    }

}